<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
 *
 * (c) 2009 by Mike Walsh
 *
 * @author Mike Walsh <mike_walsh@mindspring.com>
 * @package sandbox-soccer
 * @version $Revision$
 * @lastmodified $Author$
 * @lastmodifiedby $Date$
 *
 */

//  The TEMPLATEPATH constant can't be used for child
//  themes as it returns the parent theme path so we
//  need to figure out what SOCCERTTHEMEPATH is ...

define("SOCCERTHEMEPATH", sprintf("/%s/themes/%s",
    basename(WP_CONTENT_DIR), basename(dirname(__FILE__)))) ;

/**
 * Enqueue any external libraries and CSS needed to support theme.
 *
 * @return - void
 */
function soccer_enqueue()
{
    //  Reuqest the shipping version of jQuery be loaded
    //  as it is necessary to support the SuperFish menus.

    wp_enqueue_script('jquery') ;

    //  Load Superfish menus - it comes from within the theme

    $superfish = "%s/js/superfish-1.4.8/js/superfish.js" ;
    $hoverIntent = "%s/js/superfish-1.4.8/js/hoverIntent.js" ;
    $sfmenu = "%s/js/sf-menu.js" ;

    wp_enqueue_script('hoverIntent',
        sprintf($hoverIntent, SOCCERTHEMEPATH), array('jquery')) ;
    wp_enqueue_script('superfish',
        sprintf($superfish, SOCCERTHEMEPATH), array('jquery')) ;
    wp_enqueue_script('sfmenu',
        sprintf($sfmenu, SOCCERTHEMEPATH), array('jquery')) ;

    //  Load the CSS based on the theme options

    $theme_url = dirname(get_bloginfo('stylesheet_url')) ;

    $theme_options = explode(":", soccer_theme_get_options()) ;

    $superfish_css = sprintf("%s/css/superfish-%s.css",
        $theme_url, strtolower($theme_options[1])) ;

    $color_scheme_css = sprintf("%s/css/sandbox-soccer-%s.css",
        $theme_url, strtolower($theme_options[1])) ;

    $gender_css = sprintf("%s/css/sandbox-soccer-%s-%s.css", $theme_url,
        strtolower($theme_options[0]), strtolower($theme_options[1])) ;

    wp_register_style('superfishcss', $superfish_css) ; 
    wp_enqueue_style( 'superfishcss') ;

    wp_register_style('sanbox-soccer-color-css', $color_scheme_css) ; 
    wp_enqueue_style( 'sanbox-soccer-color-css') ;

    wp_register_style('sanbox-soccer-gender-css', $gender_css) ; 
    wp_enqueue_style( 'sanbox-soccer-gender-css') ;
}

//  Hook in a redirect action for the template to the enqueue will run

add_action('template_redirect','soccer_enqueue');

/**
 * filter to add [print_link] to every post or page
 *
 */
function soccer_theme_config_print_link_filter()
{
    //  Make sure wp-print is installed and active
    //  before adding the filter or else the short
    //  code text will be displayed as part of the
    //  post.
 
    $plugins = get_option('active_plugins') ;

    if (!empty($plugins))
    {
        foreach ($plugins as $plugin)
        {
            if (preg_match('/^.*wp-print\.php$/', $plugin))
            {
                add_filter('the_content', 'soccer_theme_add_print_link_filter') ;
                break ;
            }
        }
    }
}


/**
 * filter to add [print_link] to every post or page
 *
 * @param string - content
 * @return string - filtered content
 */
function soccer_theme_add_print_link_filter($content)
{
    return $content . "<div class=\"print-link\">[print_link]</div>" ;
}

//  Configure the print link filter

soccer_theme_config_print_link_filter() ;

//  Set up theme options - allows theme
//  to be configured with a color scheme
//  and male, female, or co-ed silhouettes

add_action('admin_menu', 'soccer_theme_options_page');

/**
 * Soccer Theme options page hook
 */
function soccer_theme_options_page()
{
    add_theme_page('soccer', 'Theme Options',
        '8', 'functions', 'soccer_theme_set_options');
}

//  Define constants to drive the theme options page.
//  These values are important and should not be changed
//  as they drive the selection of the proper header
//  image and CSS file.

define('SOCCER_THEME_GENDER_MALE', 'male') ;
define('SOCCER_THEME_GENDER_FEMALE', 'female') ;
define('SOCCER_THEME_GENDER_BOTH', 'both') ;
define('SOCCER_THEME_DEFAULT_GENDER', SOCCER_THEME_GENDER_BOTH) ;

define('SOCCER_THEME_COLOR_SCHEME_RED', 'red') ;
define('SOCCER_THEME_COLOR_SCHEME_BLUE', 'blue') ;
define('SOCCER_THEME_COLOR_SCHEME_GREEN', 'green') ;
define('SOCCER_THEME_DEFAULT_COLOR_SCHEME', SOCCER_THEME_COLOR_SCHEME_RED) ;

define('SOCCER_THEME_SILHOUETTE', 'Soccer_Silhouette_%s_%s.png') ;
define('SOCCER_THEME_SILHOUETTE_DISPLAY_WIDTH', "400px") ;
define('SOCCER_THEME_SILHOUETTE_DISPLAY_HEIGHT', "100px") ;

/**
 * get current soccer theme options
 *
 */
function soccer_theme_get_options()
{
    $current_setting = get_option('soccer_theme_options') ;

    if (empty($current_setting))
        $current_setting = ucwords(SOCCER_THEME_DEFAULT_GENDER) .
            ":" . ucwords(SOCCER_THEME_DEFAULT_COLOR_SCHEME) ;

    return $current_setting ;
}

/**
 * soccer theme options page form
 *
 */
function soccer_theme_set_options()
{
    $soccer_theme_gender_options = array(
        SOCCER_THEME_GENDER_MALE => SOCCER_THEME_GENDER_MALE
       ,SOCCER_THEME_GENDER_FEMALE => SOCCER_THEME_GENDER_FEMALE
       ,SOCCER_THEME_GENDER_BOTH => SOCCER_THEME_GENDER_BOTH
   ) ;

    $soccer_theme_color_scheme_options = array(
        SOCCER_THEME_COLOR_SCHEME_RED => SOCCER_THEME_COLOR_SCHEME_RED
       ,SOCCER_THEME_COLOR_SCHEME_BLUE => SOCCER_THEME_COLOR_SCHEME_BLUE
       ,SOCCER_THEME_COLOR_SCHEME_GREEN => SOCCER_THEME_COLOR_SCHEME_GREEN
   ) ;

    $soccer_theme_thumbnails = array() ;

    foreach ($soccer_theme_gender_options as $gender)
    {
        $url = dirname(get_bloginfo('stylesheet_url')) ;

        foreach ($soccer_theme_color_scheme_options as $color)
        {
            $img = sprintf("%s/images/%s", $url,
                sprintf(SOCCER_THEME_SILHOUETTE,
                ucwords($gender), ucwords($color))) ;
            $key = sprintf("%s:%s", ucwords($gender), ucwords($color)) ;
            $soccer_theme_thumbnails[$key] = $img ;
        }
    }

    $current_setting = get_option('soccer_theme_options') ;

    if (empty($current_setting))
        $current_setting = ucwords(SOCCER_THEME_DEFAULT_GENDER) .
            ":" . ucwords(SOCCER_THEME_DEFAULT_COLOR_SCHEME) ;
?>
<div class="wrap">
	<div id="icon-options-general" class="icon32"><br /></div>
    <h2>Sandbox Soccer Theme Options</h2>
    <form method="post" action="options.php">
    <?php wp_nonce_field('update-options') ; ?>
    <table style="border: 1px solid #eeeeee;">
<?php
    $c = 1 ;
    foreach ($soccer_theme_thumbnails as $key => $value)
    {
        $checked = ($key == $current_setting) ? "checked" : "" ;

        //  Open a row for even numbered choices.
        if ($c % 2) print "<tr>" ;
        printf("<td style=\"text-align: center; padding: 5px 0px 20px 0px;\"><img style=\"margin: 5px;\" width=\"%s\" height=\"%s\" src=\"%s\" alt=\"%s\"><br/><input title=\"%s\" type='radio' name='soccer_theme_options' value=\"%s\" %s/><strong>%s</strong></td>",
            SOCCER_THEME_SILHOUETTE_DISPLAY_WIDTH,
            SOCCER_THEME_SILHOUETTE_DISPLAY_HEIGHT, $value,
            basename($value), $key, $key, $checked, $key) ;

        $c++ ;

        //  Close a row for odd numbered choices
        if (!$c % 2) print "</tr>\n" ;
    }

    //  Need to close the last row?
    if ($c % 2) print "<td>&nbsp;</td></tr>\n" ;
?>
    </table>
  <input type="hidden" name="action" value="update" />
  <input type="hidden" name="page_options" value="soccer_theme_options" />
  <p class="submit">
  <input type="submit" name="Submit" class="button-primary" value="Save Changes" />
  </p>
  </form>
  </div>
<?php
  }
?>
